function [Table] = getBitValLoc(M)
%*------------------------------ getBitValLoc ----------------------------------%
%
% getBitValLoc
%
% This function calculate outs 3 dimension table where each layer refer
% to bit where the first row for symbols where this bit is zero value and the
% next row for symbols where this bit is one and so one for each layer and
% refer for certain bit 
%number of layers = number of bits = M
%
%*-------------------------------------------------------------------------
global  LUT_PSK_Q_8 LUT_QAM_16 LUT_QAM_32 LUT_QAM_64 

if M==3
%     load LUT_PSK_Q_8
    const=LUT_PSK_Q_8;
elseif M>3
%     eval(['load LUT_QAM_',num2str(2^M),';const=LUT_QAM_',num2str(2^M),';']);
    eval(['const=LUT_QAM_',num2str(2^M),';']);
end

%======== BITS values locations (Zeros & Ones)=========%
if M >= 1
    %====LSB=====%
    b0zeros = (0:2:(2^M)-1) + 1;
    b0ones  = (1 :2:(2^M)-1) + 1;
end

if M >= 2
    b1z = (0:4:(2^M)-1) + 1;
    b1zeros = reshape([b1z;b1z+1],1,[]);
    b1o = (2:4:(2^M)-1) + 1;
    b1ones = reshape([b1o;b1o+1],1,[]);
end

if M >=3
    b2z = (0:8:(2^M)-1) + 1;
    b2zeros = reshape([b2z;b2z+1;b2z+2;b2z+3],1,[]);
    b2o = (4:8:(2^M)-1) + 1;
    b2ones = reshape([b2o;b2o+1;b2o+2;b2o+3],1,[]);
end

if M >= 4
    b3z = (0:16:(2^M)-1) + 1;
    b3zeros = reshape([b3z;b3z+1;b3z+2;b3z+3;b3z+4;b3z+5;b3z+6;b3z+7],1,[]);
    b3o = (8:16:(2^M)-1) + 1;
    b3ones = reshape([b3o;b3o+1;b3o+2;b3o+3;b3o+4;b3o+5;b3o+6;b3o+7],1,[]);
end

if M >= 5
    b4z = (0:32:(2^M)-1) + 1;
    b4zeros = reshape([b4z;b4z+1;b4z+2;b4z+3;b4z+4;b4z+5;b4z+6;b4z+7;...
        b4z+8;b4z+9;b4z+10;b4z+11;b4z+12;b4z+13;b4z+14;b4z+15],1,[]);
    b4o = (16:32:(2^M)-1) + 1;
    b4ones = reshape([b4o;b4o+1;b4o+2;b4o+3;b4o+4;b4o+5;b4o+6;b4o+7;...
        b4o+8;b4o+9;b4o+10;b4o+11;b4o+12;b4o+13;b4o+14;b4o+15],1,[]);
end

if M >= 6
    %======MSB======%
    b5z = (0:64:(2^M)-1) + 1;
    b5zeros = reshape([b5z;b5z+1;b5z+2;b5z+3;b5z+4;b5z+5;b5z+6;b5z+7;...
        b5z+8;b5z+9;b5z+10;b5z+11;b5z+12;b5z+13;b5z+14;b5z+15;...
        b5z+16;b5z+17;b5z+18;b5z+19;b5z+20;b5z+21;b5z+22;b5z+23;...
        b5z+24;b5z+25;b5z+26;b5z+27;b5z+28;b5z+29;b5z+30;b5z+31],1,[]);
    
    b5o = (32:64:(2^M)-1) + 1;
    b5ones = reshape([b5o;b5o+1;b5o+2;b5o+3;b5o+4;b5o+5;b5o+6;b5o+7;...
        b5o+8;b5o+9;b5o+10;b5o+11;b5o+12;b5o+13;b5o+14;b5o+15;...
        b5o+16;b5o+17;b5o+18;b5o+19;b5o+20;b5o+21;b5o+22;b5o+23;...
        b5o+24;b5o+25;b5o+26;b5o+27;b5o+28;b5o+29;b5o+30;b5o+31],1,[]);
end
%===  Table(:,:,1) is MSB
%===  Table(:,:,M) is LSB
for n = 1:M
    Table(:,:,n) = eval(['[const(b',num2str(M-n),'zeros); const(b',num2str(M-n),'ones)];']);
end
